package com.gitee.ywj1352.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *
 */
public class JSONUtils {
    private final static ObjectMapper MAPPER;

    static {
        MAPPER = new ObjectMapper();
    }

    public static <T> String serialize(T obj) {
        String res = null;
        try {
            res = MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            //ignore error and return null
            return null;
        }
        return res;
    }

    public static <T> T deserialize(String jsonStr, Class<T> clazz) {
        try {
            return MAPPER.readValue(jsonStr, clazz);
        } catch (JsonProcessingException e) {
            //ignore error and return null
            return null;
        }
    }


}
